package com.xiaoxing.salesclient.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarLayout;
import com.haibin.calendarview.CalendarView;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoxing.salesclient.di.component.DaggerYuYueShiJianComponent;
import com.xiaoxing.salesclient.di.module.YuYueShiJianModule;
import com.xiaoxing.salesclient.mvp.contract.YuYueShiJianContract;
import com.xiaoxing.salesclient.mvp.model.entity.AppointTime;
import com.xiaoxing.salesclient.mvp.presenter.YuYueShiJianPresenter;
import com.xiaoxing.salesclient.mvp.ui.adapter.YuYueShiJianAdapter;
import com.xiaoxing.salesclient.mvp.ui.entity.YuYueShiJian;
import com.xiaoxing.salesclient.mvp.ui.popup.YuYueShiJianPopup;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;
import xiaoxing.com.salesclient.R;
import xiaoxing.com.salesclient.R2;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.SALES_CLIENT_YU_YUE_SHI_JIAN_ACTIVITY)
public class YuYueShiJianActivity extends BaseActivity<YuYueShiJianPresenter> implements YuYueShiJianContract.View, OnRefreshListener {


    private static boolean mIsNeedDemo = true;
    @BindView(R2.id.empty_text)
    TextView empty_text;
    @BindView(R2.id.empty_image)
    ImageView empty_image;

    @BindView(R2.id.empty)
    View mEmptyLayout;
    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R2.id.refreshLayout)
    RefreshLayout mRefreshLayout;
    private YuYueShiJianAdapter mAdapter;

    @BindView(R2.id.calendarLayout)
    CalendarLayout mCalendarLayout;

    @BindView(R2.id.calendarView)
    CalendarView mCalendarView;
    @BindView(R2.id.tv_shang_yi_zhou)
    TextView tvShangYiZhou;
    @BindView(R2.id.tv_year)
    TextView tvYear;
    @BindView(R2.id.tv_xia_yi_zhou)
    TextView tvXiaYiZhou;
    private List<AppointTime.DataBean> mDataBeanList = new ArrayList<>();
    private boolean isFirst = true;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerYuYueShiJianComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .yuYueShiJianModule(new YuYueShiJianModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_yu_yue_shi_jian; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, "預約時間");

        initRefreshLayout();
        initRecyclerView();
        initEmpty();


        mCalendarView.setOnDateSelectedListener(new CalendarView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(Calendar calendar, boolean isClick) {


//                ArmsUtils.snackbarText(calendar.getYear() + "-" + calendar.getMonth() + "-" + calendar.getDay());

//                Utils.navigation(YuYueShiJianActivity.this, RouterHub.SALES_CLIENT_APPOINT_TIME_ACTIVITY);
                if (!isFirst) {
                    isFirst = false;
                    ARouter.getInstance().build(RouterHub.SALES_CLIENT_APPOINT_TIME_ACTIVITY).withString("date", calendar.getYear() + "-" + calendar.getMonth() + "-" + calendar.getDay()).navigation();
                } else {
                    isFirst = false;
                }
            }
        });

    }

    private void initRefreshLayout() {
        mRefreshLayout.setRefreshHeader(new ClassicsHeader(this).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(R.color.public_colorPrimary).setAccentColorId(android.R.color.white));
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.autoRefresh();
        mRefreshLayout.setEnableLoadMore(false);
    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new GridLayoutManager(YuYueShiJianActivity.this, 3));
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(YuYueShiJianActivity.this, VERTICAL));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new YuYueShiJianAdapter(YuYueShiJianActivity.this, mDataBeanList));

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                new YuYueShiJianPopup(YuYueShiJianActivity.this).showPopupWindow();
            }
        });
    }

    private void initEmpty() {
        empty_image.setImageResource(R.drawable.no_data);
        empty_text.setTextColor(getResources().getColor(R.color.public_no_data));
        empty_text.setText("暫無消息,下拉更新");
    }

    /**
     * 模拟数据
     */
    private List<YuYueShiJian> loadModels() {
        List<YuYueShiJian> addressLists = new ArrayList<>();

        for (int i = 0; i < 5; i++) {

            YuYueShiJian addressList = new YuYueShiJian();
            addressLists.add(addressList);
        }


        return addressLists;
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

        mPresenter.appointTime();
        mRefreshLayout.finishRefresh();
        mEmptyLayout.setVisibility(View.GONE);

    }

    @OnClick({R2.id.tv_shang_yi_zhou, R2.id.tv_year, R2.id.tv_xia_yi_zhou})
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.tv_shang_yi_zhou) {
            isFirst = true;
            mCalendarView.scrollToPre(true);
//            ArmsUtils.snackbarText("只能選當月或下個月");
        } else if (i == R.id.tv_year) {
            if (mCalendarLayout.isExpand()) {
                mCalendarLayout.shrink();
            } else {
                mCalendarLayout.expand();
            }
        } else if (i == R.id.tv_xia_yi_zhou) {
            isFirst = true;
            mCalendarView.scrollToNext(true);
//            ArmsUtils.snackbarText("只能選當月");
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void appointTimeSuccess(AppointTime appointTime) {

        if (appointTime != null && appointTime.getData() != null && appointTime.getData().getList().size() > 0) {

        }
    }
}
